package com.jia.coupon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.commons.sku.MemberPrice;
import com.jia.commons.sku.SkuReduction;
import com.jia.coupon.entity.SkuFullReduction;
import com.jia.coupon.entity.SkuLadder;
import com.jia.coupon.mapper.SkuFullReductionMapper;
import com.jia.coupon.service.MemberPriceService;
import com.jia.coupon.service.SkuFullReductionService;
import com.jia.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品满减信息(SkuFullReduction)表服务实现类
 *
 * @author makejava
 * @since 2025-07-28 17:36:23
 */
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper,
        SkuFullReduction> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public void saveSkuReduction(SkuReduction skuReduction) {

        // 7 sku的优惠 满减 sms_sku_ladder sms_sku_full_reduction sms_member_price
        Long skuId = skuReduction.getSkuId();

        SkuLadder skuLadder = new SkuLadder();
        skuLadder.setSkuId(skuId);
        skuLadder.setFullCount(skuReduction.getFullCount());
        skuLadder.setDiscount(skuReduction.getDiscount());
        skuLadder.setAddOther(skuReduction.getCountStatus());
        skuLadder.setPrice(skuReduction.getFullPrice());
        skuLadderService.save(skuLadder);


        SkuFullReduction skuFullReduction = new SkuFullReduction();
        skuFullReduction.setSkuId(skuId);
        skuFullReduction.setFullPrice(skuReduction.getFullPrice());
        skuFullReduction.setReducePrice(skuReduction.getReducePrice());
        skuFullReduction.setAddOther(skuReduction.getCountStatus());
        this.saveOrUpdate(skuFullReduction);


        List<MemberPrice> memberPrices = skuReduction.getMemberPrices();

        List<com.jia.coupon.entity.MemberPrice> collect = memberPrices.stream().map(mp -> {
            com.jia.coupon.entity.MemberPrice entity = new com.jia.coupon.entity.MemberPrice();
            entity.setSkuId(skuId);
            entity.setMemberPrice(mp.getPrice());
            entity.setMemberLevelId(mp.getId());
            entity.setMemberLevelName(mp.getName());
            entity.setAddOther(1);
            return entity;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(collect);


    }

}

